﻿using System;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using SelfService.Helpers;
using SelfService.Model.Interface;
using SelfService.Model.Models;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;

namespace SelfService.Controllers;

[AllRights]
[AuthorizeJwtWithCookie]
[ActionDescription("发票打印")]
[ApiController]
[Route("api/SysReceipt")]
public class SysReceiptController : BaseApiController
{
    
    [HttpPost("GetReceipts")]
    public async Task<Result<ReceiptInfo[]>> GetReceipts([FromBody] SysPatient patient)
    {
        var action = "查询发票列表";
        IReceiptPrint service;
        try
        {
            var scope = Wtm.ServiceProvider.CreateScope();
            service = scope.ServiceProvider.GetRequiredService<IReceiptPrint>();
        }
        catch (Exception)
        {
            return new Result<ReceiptInfo[]>()
            {
                Code = 1,
                Message = "服务端没有启用该业务"
            };
        }
        this.Logger($"{action}:{JsonSerializer.Serialize(patient)}", ActionLogTypesEnum.Debug);
        try
        {
            var result = await service.GetList(patient);
            if (result.Code == 200)
            {
                return new Result<ReceiptInfo[]>()
                {
                    Data = result.Data.ToArray()
                };
            }
            else
            {
                return new Result<ReceiptInfo[]>()
                {
                    Code = 1,
                    Message = result.Message
                };
            }
        }
        catch (Exception ex)
        {
            return new Result<ReceiptInfo[]>()
            {
                Code = 1,
                Message = ex.Message
            };
        }
    }
    
    [HttpGet("GetDetail")]
    public async Task<Result<ReceiptInfo>> GetDetail([FromQuery] string tradeNo)
    {
        var action = "查询发票信息";
        IReceiptPrint service;
        try
        {
            var scope = Wtm.ServiceProvider.CreateScope();
            service = scope.ServiceProvider.GetRequiredService<IReceiptPrint>();
        }
        catch (Exception)
        {
            return new Result<ReceiptInfo>()
            {
                Code = 1,
                Message = "服务端没有启用该业务"
            };
        }
        this.Logger($"{action}:{tradeNo}", ActionLogTypesEnum.Debug);
        try
        {
            var result = await service.GetDetail(tradeNo);
            if (result.Code == 200)
            {
                return new Result<ReceiptInfo>()
                {
                    Data = result.Data
                };
            }
            else
            {
                return new Result<ReceiptInfo>()
                {
                    Code = 1,
                    Message = result.Message
                };
            }
        }
        catch (Exception ex)
        {
            return new Result<ReceiptInfo>()
            {
                Code = 1,
                Message = ex.Message
            };
        }
    }
}